<?php
// 引入数据库连接文件和组件
include 'db_connect.php';
include 'components/Utils.php';
include 'components/AddEditRecord.php';
include 'components/FilterRecords.php';
include 'components/RecordList.php';
include 'ManageExerciseSets.php'; // 包含习题集管理功能

// 处理检查重复记录的AJAX请求
if (isset($_POST['check_duplicate'])) {
    $exam_date = $_POST['exam_date'];
    $question_type = $_POST['question_type'] ?? '';
    $exercise_set_id = $_POST['exercise_set_id'];
    $chapter_id = $_POST['chapter_id'];
    $question_number = $_POST['question_number'];
    $start_time = $_POST['start_time'] ?? '';
    $end_time = $_POST['end_time'] ?? '';
    
    // 调用检查重复记录的函数
    $is_duplicate = checkDuplicateRecord($conn, $exam_date, $question_type, $exercise_set_id, $chapter_id, $question_number, $start_time, $end_time);
    
    // 返回JSON格式的结果
    echo json_encode(['is_duplicate' => $is_duplicate]);
    exit();
}

// 计算做题耗时函数（从Utils.php引入）

// 处理导出Excel（会在需要时退出脚本）
handleExportExcel($conn);

// 处理添加记录后重定向（修复刷新重复提交bug）
handleAddRecordRedirection($conn);

// 处理更新记录后重定向
handleUpdateRecordRedirection($conn);

// 处理删除记录后重定向
handleDeleteRecordRedirection($conn);

// 处理习题集相关请求（必须在HTML输出前调用）
handleExerciseSetsRequests($conn);

// 编辑记录时获取数据
$edit_record = null;
if (isset($_GET['edit'])) {
    $id = $_GET['edit'];
    $sql = "SELECT * FROM exam_records WHERE id=?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $result = $stmt->get_result();
    $edit_record = $result->fetch_assoc();
    $stmt->close();
}

// 确定当前活动的标签页
$active_tab = isset($_GET['tab']) ? $_GET['tab'] : 'add';
if (!in_array($active_tab, ['add', 'list', 'exercise_sets'])) {
    $active_tab = 'add';
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>刷题归档System</title>
    <!-- 黑色铅笔图标 -->
    <link rel="icon" type="image/png" href="assets/favicon.png">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <style>
        /* Slate shadcn style variables */
        :root {
            --slate-50: #f8fafc;
            --slate-100: #f1f5f9;
            --slate-200: #e2e8f0;
            --slate-300: #cbd5e1;
            --slate-400: #94a3b8;
            --slate-500: #64748b;
            --slate-600: #475569;
            --slate-700: #334155;
            --slate-800: #1e293b;
            --slate-900: #0f172a;
            --slate-950: #020617;
            --primary-50: #f0f9ff;
            --primary-100: #e0f2fe;
            --primary-200: #bae6fd;
            --primary-300: #7dd3fc;
            --primary-400: #38bdf8;
            --primary-500: #0ea5e9;
            --primary-600: #0284c7;
            --primary-700: #0369a1;
            --primary-800: #075985;
            --primary-900: #0c4a6e;
        }

        body {
            font-family: 'Inter', 'Microsoft YaHei', sans-serif;
            background-color: var(--slate-50);
            color: var(--slate-800);
            line-height: 1.6;
            margin: 0;
            padding-top: 150px; /* 为固定的标题栏和tab栏留出空间 */
        }

        .container {
            max-width: 1400px;
            padding: 0 1rem;
            margin: 0 auto;
        }

        /* 固定标题栏容器 */
        .fixed-header {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            background-color: var(--slate-50);
            z-index: 1000;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            padding: 0.5rem 0;
        }

        .fixed-header .container {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        /* 固定tab栏 */
        .tabbar {
            margin-bottom: 0;
        }

        /* Card styles */
        .card {
            background-color: white;
            border: 1px solid var(--slate-200);
            border-radius: 12px;
            margin-bottom: 24px;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
            transition: all 0.2s ease;
        }

        .card:hover {
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            transform: translateY(-1px);
        }

        .card-header {
            background-color: var(--slate-800);
            color: var(--slate-100);
            border-radius: 12px 12px 0 0;
            padding: 1rem 1.5rem;
            border: none;
            font-weight: 600;
        }

        .card-header h3 {
            color: var(--slate-100);
            margin-bottom: 0;
            font-size: 1.25rem;
            font-weight: 600;
        }

        .card-body {
            padding: 1.5rem;
        }

        /* Form styles */
        .form-label {
            font-weight: 500;
            color: var(--slate-700);
            margin-bottom: 0.5rem;
            display: block;
        }

        .form-control {
            background-color: white;
            border: 1px solid var(--slate-300);
            border-radius: 8px;
            padding: 0.75rem 1rem;
            transition: all 0.2s ease;
            color: var(--slate-900);
            font-weight: 500;
        }

        .form-control:focus {
            outline: none;
            border-color: var(--primary-500);
            box-shadow: 0 0 0 3px rgba(14, 165, 233, 0.1);
            background-color: white;
            color: var(--slate-950);
        }

        .form-control::placeholder {
            color: var(--slate-400);
        }

        /* Button styles */
        .btn {
            border-radius: 8px;
            padding: 0.75rem 1.5rem;
            font-weight: 500;
            transition: all 0.2s ease;
            border: none;
            cursor: pointer;
            font-size: 0.875rem;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 0.5rem;
        }

        .btn-primary {
            background-color: var(--primary-600);
            color: white;
        }

        .btn-primary:hover {
            background-color: var(--primary-700);
            transform: translateY(-1px);
        }

        .btn-secondary {
            background-color: var(--slate-200);
            color: var(--slate-700);
        }

        .btn-secondary:hover {
            background-color: var(--slate-300);
            color: var(--slate-800);
        }

        .btn-success {
            background-color: var(--primary-600);
            color: white;
        }

        .btn-success:hover {
            background-color: var(--primary-700);
        }

        .btn-warning {
            background-color: #f59e0b;
            color: white;
        }

        .btn-warning:hover {
            background-color: #d97706;
        }

        .btn-danger {
            background-color: #ef4444;
            color: white;
        }

        .btn-danger:hover {
            background-color: #dc2626;
        }

        .btn-sm {
            padding: 0.5rem 0.75rem;
            font-size: 0.75rem;
        }

        /* Table styles */
        .table {
            border-collapse: separate;
            border-spacing: 0;
            width: 100%;
            border-radius: 8px;
            overflow: hidden;
        }

        .table th {
            background-color: var(--slate-100);
            color: var(--slate-900);
            font-weight: 600;
            text-align: left;
            padding: 1rem;
            border-bottom: 2px solid var(--slate-200);
        }

        .table td {
            padding: 1rem;
            border-bottom: 1px solid var(--slate-200);
            background-color: white;
            color: var(--slate-900);
            font-weight: 400;
        }

        .table tr:last-child td {
            border-bottom: none;
        }

        .table-hover tbody tr:hover td {
            background-color: var(--slate-50);
        }

        .table-striped tbody tr:nth-of-type(odd) td {
            background-color: var(--slate-50);
        }

        .table-responsive {
            overflow-x: auto;
            border-radius: 8px;
        }

        /* Typography */
        h1, h2, h3, h4, h5, h6 {
            color: var(--slate-900);
            font-weight: 600;
            margin-bottom: 1rem;
        }

        h1 {
            font-size: 2.25rem;
            line-height: 1.2;
        }

        h3 {
            font-size: 1.5rem;
            line-height: 1.3;
        }

        .text-center {
            text-align: center;
        }

        .mb-4 {
            margin-bottom: 1.5rem;
        }

        .mb-1 {
            margin-bottom: 0.25rem;
        }

        .mt-2 {
            margin-top: 0.5rem;
        }

        .ms-2 {
            margin-left: 0.5rem;
        }

        /* Layout utilities */
        .row {
            display: flex;
            flex-wrap: wrap;
            gap: 1rem;
            margin-bottom: 1rem;
        }

        .col-12, .col-md-4, .col-md-3, .col-md-6 {
            flex: 1;
            min-width: 0;
        }

        .col-12 {
            width: 100%;
        }

        @media (min-width: 768px) {
            .col-md-3 {
                flex: 0 0 auto;
                width: 25%;
            }
            
            .col-md-4 {
                flex: 0 0 auto;
                width: calc(33.333% - 0.666rem);
            }
            
            .col-md-6 {
                flex: 0 0 auto;
                width: calc(50% - 0.5rem);
            }
        }

        .d-flex {
            display: flex;
        }

        .justify-content-between {
            justify-content: space-between;
        }

        .align-items-center {
            align-items: center;
        }

        .gap-3 {
            gap: 1rem;
        }

        /* Highlight */
        .highlight {
            background-color: #fef3c7;
        }
    </style>
</head>
<body>
    <div class="fixed-header">
        <div class="container">
            <div style="display: flex; justify-content: space-between; align-items: center; width: 100%;">
                <h1 style="font-size: 1.5rem; margin: 0;">刷题归档System</h1>
                
                <!-- Tabbar导航 -->
                <div style="flex-shrink: 0;">
                    <?php echo renderTabbar($active_tab); ?>
                </div>
            </div>
        </div>
    </div>
    
    <div class="container">
        
        <!-- 根据当前活动标签页显示相应组件 -->
        <?php
        // 根据当前活动的标签页显示相应的内容
        if ($active_tab == 'add') {
            // 显示添加/编辑记录组件
            echo renderAddEditRecord($conn, $edit_record);
        } elseif ($active_tab == 'list') {
            // 合并显示筛选组件和记录列表组件
            echo renderFilterRecords($conn);
            echo renderRecordList($conn);
        } elseif ($active_tab == 'exercise_sets') {
            // 显示习题集管理内容
            echo renderExerciseSetsManagement($conn);
        }
        ?>
    </div>
    <script>
        // 页面加载完成后执行
        document.addEventListener('DOMContentLoaded', function() {
            // 只有在添加新记录页面且没有编辑模式时才设置当前时间
            <?php if (!$edit_record && $active_tab == 'add'): ?>
                // 获取当前时间
                const now = new Date();
                const hours = now.getHours().toString().padStart(2, '0');
                const minutes = now.getMinutes().toString().padStart(2, '0');
                const currentTime = hours + ':' + minutes;
                
                // 设置开始时间和结束时间为当前时间
                if (document.getElementById('start_time') && document.getElementById('end_time')) {
                    document.getElementById('start_time').value = currentTime;
                    document.getElementById('end_time').value = currentTime;
                }
            <?php endif; ?>
        });
    </script>
</body>
</html>
<?php
// 关闭数据库连接
close_connection($conn);
?>